/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaTecnologia.java
 *
 * Created on 02-jun-2012, 0:57:32
 */
package edu.eam.gruposenior.gestorLEcciones.vista;

import edu.eam.gruposenior.gestorLEcciones.controladores.TecnologiaController;
import edu.eam.gruposenior.gestorLEcciones.entidades.Tecnologia;

import javax.swing.JOptionPane;

/**
 * 
 * @author Claudia
 */
public class VentanaTecnologia extends javax.swing.JFrame {

	/** Creates new form VentanaTecnologia */
	TecnologiaController control;

	public VentanaTecnologia() {
		initComponents();
		control = new TecnologiaController();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		tfNOmbre = new javax.swing.JTextField();
		tfDescripcion = new javax.swing.JTextField();
		bCrear = new javax.swing.JButton();
		bEditar = new javax.swing.JButton();
		bBuscar = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		jLabel1.setText("Nombre");

		jLabel2.setText("Desscripcion");

		bCrear.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/Add.png"))); // NOI18N
		bCrear.setText("Crear");
		bCrear.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				bCrearActionPerformed(evt);
			}
		});

		bEditar.setIcon(new javax.swing.ImageIcon(
				getClass()
						.getResource(
								"/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/Notepad.png"))); // NOI18N
		bEditar.setText("Editar");
		bEditar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				bEditarActionPerformed(evt);
			}
		});

		bBuscar.setIcon(new javax.swing.ImageIcon(
				getClass()
						.getResource(
								"/edu/eam/gruposenior/gestorLEcciones/vista/imagenes/Search.png"))); // NOI18N
		bBuscar.setText("Buscar");
		bBuscar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				bBuscarActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addComponent(
														jLabel2,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(jLabel1))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														javax.swing.GroupLayout.Alignment.TRAILING,
														layout.createSequentialGroup()
																.addComponent(
																		bCrear)
																.addPreferredGap(
																		javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																		33,
																		Short.MAX_VALUE)
																.addComponent(
																		bEditar)
																.addGap(18, 18,
																		18)
																.addComponent(
																		bBuscar))
												.addComponent(
														tfDescripcion,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														320, Short.MAX_VALUE)
												.addComponent(
														tfNOmbre,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														320, Short.MAX_VALUE))
								.addContainerGap()));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addContainerGap(34, Short.MAX_VALUE)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(jLabel1)
												.addComponent(
														tfNOmbre,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														tfDescripcion,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(jLabel2))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(bBuscar)
												.addComponent(bCrear)
												.addComponent(bEditar))
								.addGap(29, 29, 29)));

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void bCrearActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_bCrearActionPerformed
		// TODO add your handling code here:con

		Tecnologia tec = control.buscarTecnologia(tfNOmbre.getText());

		if (tec == null) {

			if (!tfNOmbre.getText().equals("")
					&& !tfDescripcion.getText().equals("")) {

				control.crearTecnologia(tfNOmbre.getText(),
						tfDescripcion.getText());
				tfNOmbre.setText("");
				tfDescripcion.setText("");
				JOptionPane.showMessageDialog(null, "Creada con exito");
			} else {
				JOptionPane.showMessageDialog(null,
						"Hay campos que no tienen valor", "Campos vacios",
						JOptionPane.ERROR_MESSAGE);
			}

		} else {
			JOptionPane.showMessageDialog(null, "La tecnologia ya existe");
		}

	}// GEN-LAST:event_bCrearActionPerformed

	private void bEditarActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_bEditarActionPerformed

		try {
			if (!tfNOmbre.getText().equals("")
					&& !tfDescripcion.getText().equals("")) {

				control.editarTecnologia(tfNOmbre.getText(),
						tfDescripcion.getText());

				tfNOmbre.setText("");
				tfDescripcion.setText("");
				JOptionPane.showMessageDialog(null, "Editada con exito");
			} else {
				JOptionPane.showMessageDialog(null,
						"Hay campos que no tienen valor", "Campos vacios",
						JOptionPane.ERROR_MESSAGE);
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage(),
					"Tecnologia Inexistente", JOptionPane.ERROR_MESSAGE);
		}

	}// GEN-LAST:event_bEditarActionPerformed

	private void bBuscarActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_bBuscarActionPerformed
		Tecnologia tec = control.buscarTecnologia(tfNOmbre.getText());

		if (tec != null) {
			tfDescripcion.setText(tec.getDescripcion());
		} else {
			JOptionPane.showMessageDialog(null, "La tecnologia no existe");
		}
	}// GEN-LAST:event_bBuscarActionPerformed

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new VentanaTecnologia().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JButton bBuscar;
	private javax.swing.JButton bCrear;
	private javax.swing.JButton bEditar;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JTextField tfDescripcion;
	private javax.swing.JTextField tfNOmbre;
	// End of variables declaration//GEN-END:variables
}
